import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;

public class MybatisDemo06 {
    public static void main(String[] args) throws IOException {
        User user = new User();
        user.setId(5);
        user.setUsername("xiaohe");
        user.setFullname("贺振亚");
        user.setPassword("666777");
        user.setAddress("山东省青岛市市南区");
        //操作Mybatis的四个步骤
        //1.加载mybatis-config.xml配置文件，创建工厂类
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //2.创建SqlSession
        SqlSession sqlSession = factory.openSession();
        //3.执行SQL语句
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.updateOne(user);
        sqlSession.commit();//插入、修改、删除，都需要提交事务
        //4.关闭SqlSession
        sqlSession.close();

    }
}

